package airport.user.action;

//import airport.user.pojo.User;
import airport.user.dao.UserDAO;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class ChangeImageAction extends ActionSupport implements
		ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	private String avatar;
	//private String type;

	private HttpServletRequest servletRequest;

	public String execute() {
		/**
		 * get the path of the uploaded file
		 */
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(cookieName)) {
					username = cookies[i].getValue();
					break;
				}
			}
		}
		
		UserDAO dao = new UserDAO();
		if (avatar.equals("dog"))
			dao.updateAvatar("images/dog.jpg", username);
		else
			if (avatar.equals("cat"))
				dao.updateAvatar("images/cat.jpg", username);
			else
				if (avatar.equals("car"))
					dao.updateAvatar("images/car.jpg", username);
		return SUCCESS;
	}
	
	

	
	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;

	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}
}

